# -*- coding: utf-8 -*-
# @Date   : 2019/12/10
# @File   : R21_IC_UpaidNonExternalGuaranteeBusinessOfBadCheck_WHB_2D.py
# @Author : zhaochen


'''eof
name:申请人未结清非对外担保业务校验（不良类）
code:R21_IC_UpaidNonExternalGuaranteeBusinessOfBadCheck_WHB_2D
tableName:
columnName:
groups:弱规则>个人征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def R21_IC_UpaidNonExternalGuaranteeBusinessOfBadCheck_WHB_2D():
    data = IC_DSJ_2D['data']
    factor1 = []
    factor2 = []
    # 借贷账户信息
    PDA = getTable(data, 'PDA', 1)
    if PDA is None:
        return u'缺失值'
    elif PDA == [{}] or PDA == "":
        return 0
    else:
        # 借贷账户信息单元
        PD01 = getTable(PDA[0], 'PD01', 0)
        if PD01 is None or PD01 == "":
            return u'缺失值'
        elif PD01 == [{}]:
            return 0
        else:
            for i in PD01:
                try:
                    # 账户基本信息
                    PD01A = getTable(i, 'PD01A', 1)
                    # 最新表现信息段
                    PD01B = getTable(i, 'PD01B', 1)
                    # 最近一次月度表现信息段
                    PD01C = getTable(i, 'PD01C', 0)
                    # # 最新表现信息段
                    if PD01B in null_type_list:
                        factor1.append(None)
                    else:
                        # 五级分类
                        PD01BD03 = PD01B[0].get('PD01BD03')
                        PD01BJ01 = PD01B[0].get('PD01BJ01')
                        if PD01BD03 in null_type_list or isfloat(PD01BJ01) is False:
                            if PD01A in null_type_list:
                                factor1.append(None)
                            else:
                                # 账户类型
                                PD01AD01 = PD01A[0].get('PD01AD01')
                                # 账户状态
                                PD01BD01 = PD01B[0].get('PD01BD01')

                                if PD01AD01 in ['D1', 'R1', 'R4'] and PD01BD01 == '4':
                                    factor1.append(0)
                                elif PD01AD01 in ['R2', 'R3'] and PD01BD01 == '5':
                                    factor1.append(0)
                                elif PD01AD01 == 'D1' and PD01BD01 in ['3', '5']:
                                    factor1.append(0)
                                elif PD01AD01 in ['R1', 'R4'] and PD01BD01 == '3':
                                    factor1.append(0)
                                elif PD01AD01 in ['R2', 'R3'] and PD01BD01 == '4':
                                    factor1.append(0)
                                elif PD01AD01 == 'C1'  and PD01BD01 == '2':
                                    factor1.append(0)
                                else:
                                    factor1.append(None)
                        else:
                            #  （3(次级),4(可疑),5(损失))
                            if PD01BD03 in ['3', '4', '5'] and float(PD01BJ01) > 0:
                                factor1.append(1)
                            else:
                                factor1.append(0)
                    # # 最近一次月度表现信息段
                    if PD01C in null_type_list:
                        factor2.append(None)
                    elif PD01C == [{}]:
                        factor2.append(0)
                    else:
                        # 五级分类
                        PD01CD02 = PD01C[0].get('PD01CD02')
                        # 余额
                        PD01CJ01 = PD01C[0].get('PD01CJ01')
                        if PD01CD02 in null_type_list or isfloat(PD01CJ01) is False:
                            factor2.append(None)
                        else:
                            # （3(次级),4(可疑),5(损失)),余额>0
                            if PD01CD02 in ['3', '4', '5'] and float(PD01CJ01) > 0:
                                factor2.append(1)
                            else:
                                factor2.append(0)
                except:
                    factor1.append(None)
                    factor2.append(None)
                    continue

    factor1NotNone = filter(lambda x: x is not None, factor1)
    factor2NotNone = filter(lambda x: x is not None, factor2)
    if len(factor1NotNone) == 0 and len(factor2NotNone) == 0:
        return u'缺失值'
    else:
        return max(sum(factor1NotNone), sum(factor2NotNone))


result = R21_IC_UpaidNonExternalGuaranteeBusinessOfBadCheck_WHB_2D()